<?php
function admin_load_template( $template_name = 'index.html' ) {
    require_once '../library/Twig/Autoloader.php';
    Twig_Autoloader :: register();
    $loader = new Twig_Loader_Filesystem( BBS_ROOT . 'templates' );
    $twig = new Twig_Environment( $loader, array( 'cache' => false
            ) );
    $escaper = new Twig_Extension_Escaper( true );
    $twig -> addExtension( $escaper );
    $template = $twig -> loadTemplate( 'admin' . DIRECTORY_SEPARATOR . $template_name );
    return $template;
}

function front_load_template( $template_name = 'index.html' ) {
    require_once 'library/Twig/Autoloader.php';
    Twig_Autoloader :: register();
    $loader = new Twig_Loader_Filesystem( 'templates' );
    $twig = new Twig_Environment( $loader, array( 'cache' => false // BBS_ROOT.'compilation_cache'
            ) );
    $escaper = new Twig_Extension_Escaper( true );
    $twig -> addExtension( $escaper );
    $template = $twig -> loadTemplate( 'front' . DIRECTORY_SEPARATOR . $template_name );
    return $template;
}

function enBbcode( $str, $key ) {
    $bbtype = array( 'member' => "BBMEMBER",
        'title' => "BBTITLE",
        'content' => "BBCONTENT",
        'date' => "BBDATE",
        'url' => "URL"
        );
    return "[$bbtype[$key]]$str" . '[/' . "$bbtype[$key]]";
}

function deBbcode( $str, $key ) {
    $bbtype = array( 'member' => "/\[BBMEMBER\](.*?)\[\/BBMEMBER\]/is",
        'title' => "/\[BBTITLE\](.*?)\[\/BBTITLE\]/is",
        'content' => "/\[BBCONTENT\](.*?)\[\/BBCONTENT\]/is",
        'date' => "/\[BBDATE\](.*?)\[\/BBDATE\]/is",
        'url' => "/\[URL\](.*?)\[\/URL\]/is"
        );
    // echo $bbtype[$key];
    preg_match_all( $bbtype[$key], $str, $matches );
    return $matches[1][0];
}
// 截取中文字符串
function cut_str( $string, $len = 10 ) {
    if ( mb_strlen( $string, 'utf-8' ) > $len ) {
        return mb_substr( $string, 0, $len-2, 'utf-8' ) . '...';
    } else
        return $string;
}
// 输出错误404模板
function show_error( $data = array() ) {
    $data['title'] = '发生错误';
    if ( !$data['error'] ) {
        $data['error'] = '您所访问的页面不存在!点击<a href="./">这里</a>返回首页';
    }
    windowNoCache( true );
    load_view( 'error.htm', $data );
}
// 显示提示信息模板
function show_info( $data = array() ) {
    if ( !$data['info'] ) {
        $data['info'] = '操作成功!';
    }
    windowNoCache( true );
    load_view( 'error.htm', $data );
}

function clear_post( &$data ) {
    $keys = array_keys( $data );
    foreach( $keys as $key ) {
        $data[$key] = addslashes( htmlspecialchars( trim( $data[$key] ) ) );
        if ( $data[$key] == null ) $data[$key] = '';
    }
}

// 不在客户端缓存
function windowNoCache() {
    if ( headers_sent() ) return ;
    header( 'Expires: ' . date( 'D,d M Y H:i:s', mktime( 0, 0, 0, 1, 1, 2000 ) ) . ' GMT' );
    header( 'Last-Modified:' . gmdate( 'D,d M Y H:i:s' ) . ' GMT' );
    header( 'Cache-control: private, no-cache,must-revalidate' );
    header( 'Pragma: no-cache' );
}

function printf_r( $var ) {
    echo '<pre>';
    var_dump( $var );
    echo '</pre>';
}

function get_base_url( $pagecode ) {
    if ( preg_match( "/<base\s+href\s*\=\s*([\"|\']?)([^\\1]*?)\\1\s*\/?>/ismu", $pagecode, $matches ) )
        return $matches[2];
    else
        return '';
}

function formatSize( $size ) {
    if ( $size > 1024 * 1024 )
        return ( $size / 1024 / 1024 ) . 'Mb';
    elseif ( $size > 1024 )
        return ( $size / 1024 ) . 'Kb';
    else
        return ( $size ) . 'b';
}

function easydate( $adate ) {
  $GMT = +8;
  return date("Y-m-d H:i:s",$adate + $GMT * 3600);
}
//
function set_flash($flash_type,$msg='') {
  setcookie($flash_type,$msg,time()+3600,'/');
}
?>